package org.jeecg.modules.xs.service.impl;

import org.jeecg.modules.xs.entity.PrivatePool;
import org.jeecg.modules.xs.entity.Order;
import org.jeecg.modules.xs.mapper.OrderMapper;
import org.jeecg.modules.xs.mapper.PrivatePoolMapper;
import org.jeecg.modules.xs.service.IPrivatePoolService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 私域池
 * @Author: sss
 * @Date:   2022-02-15
 * @Version: V1.0
 */
@Service
public class PrivatePoolServiceImpl extends ServiceImpl<PrivatePoolMapper, PrivatePool> implements IPrivatePoolService {

	@Autowired
	private PrivatePoolMapper privatePoolMapper;
	@Autowired
	private OrderMapper orderMapper;
	
	@Override
	@Transactional
	public void saveMain(PrivatePool privatePool, List<Order> orderList) {
		privatePoolMapper.insert(privatePool);
		if(orderList!=null && orderList.size()>0) {
			for(Order entity:orderList) {
				//外键设置
				entity.setPoolId(privatePool.getId());
				orderMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(PrivatePool privatePool,List<Order> orderList) {
		privatePoolMapper.updateById(privatePool);
		
		//1.先删除子表数据
		orderMapper.deleteByMainId(privatePool.getId());
		
		//2.子表数据重新插入
		if(orderList!=null && orderList.size()>0) {
			for(Order entity:orderList) {
				//外键设置
				entity.setPoolId(privatePool.getId());
				orderMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		orderMapper.deleteByMainId(id);
		privatePoolMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			orderMapper.deleteByMainId(id.toString());
			privatePoolMapper.deleteById(id);
		}
	}
	
}
